<template>
  <el-dialog title="编辑学校" :visible.sync="isEditSchool" width="30%" :before-close="handleClose" :close-on-click-modal='false'>
    <el-form ref="form" :model="form" label-width="80px" size="small">
      <el-form-item label="学校名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="所属区域">
        <el-cascader :options="area" v-model="form.area" :props="{value:'id',label:'name',children:'child'}"> </el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="checkFn()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {APIEDITSCHOOL, APIPLACE} from '@/common/api.js'
export default {
  props: ['isEditSchool', 'row'],
  data () {
    return {
      form: {
        name: '',
        area: []
      },
      area: []
    }
  },
  created () {
    this.form.name = this.row.title
    this.areaFn()
  },
  methods: {
    areaFn () {
      APIPLACE().then((res) => {
        if (res.status === 1) {
          this.area = res.data
          this.form.area.push(this.row.province)
          this.form.area.push(this.row.city)
          this.form.area.push(this.row.area)
        } else {
          this.$message.error(res.msg)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    handleChange (value) {
      console.log(value)
    },
    handleClose () {
      this.$emit('noEditSchool')
    },
    close (type) {
      this.$emit('noEditSchool', type)
    },
    checkFn () {
      if (this.form.section_title === '') {
        this.$message.warning('班级名称不能为空')
        return false
      }
      this.submit()
    },
    submit () { // 提交
      APIEDITSCHOOL({
        school_id: this.row.id,
        school_title: this.form.name,
        province: this.form.area[0],
        city: this.form.area[1],
        area: this.form.area[2]
      }).then((res) => {
        if (res.status === 1) {
          this.$message.success(res.msg)
          sessionStorage.setItem('title', this.form.name)
          location.reload()
          this.close(1)
        } else {
          this.$message.error(res.msg)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
